package com.xs.tool.controller.api;

import com.xs.tool.common.base.BasicController;
import com.xs.tool.common.jwt.JwtToken;
import com.xs.tool.model.ToolTest;
import com.xs.tool.service.ToolTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "测试")
@RestController
@RequestMapping("api/tt")
public class ToolTestController extends BasicController {
    @Autowired
    ToolTestService toolTestService;

    /**
     * @return
     * @note save
     * @date 202011070936
     * @Author xs
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "save", notes = "save")
    @JwtToken
    @ResponseBody
    public ToolTest save(
            @ModelAttribute ToolTest toolTest
    ) {
        toolTest.setFirstName(System.currentTimeMillis() + "");
        toolTest.setLastName(System.currentTimeMillis() + "");
        toolTest.setCreateDate(new Date());
        return toolTestService.save(toolTest);
    }

    /**
     * @return
     * @note save
     * @date 202011070936
     * @Author xs
     */
    @RequestMapping(value = "/query/{lastName}", method = RequestMethod.GET)
    @Transactional(readOnly = true)
    @ApiOperation(value = "query", notes = "query")
    @JwtToken
    @ResponseBody
    public Page<ToolTest> findByLastNameWithPageable(@PathVariable String lastName) {
        List<String> stringList = new ArrayList<>();
        stringList.add("id");
        try {
            Sort sort = Sort.by(Sort.Direction.DESC, "id");
            Pageable pageable = PageRequest.of(0, 10, sort);
            return toolTestService.findByLastNameWithPageable(lastName, pageable);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
